#include "UsrAI.h"
#include <cstring>  // for memcpy
#include <algorithm>
#include <cmath>
#include <string>

using namespace std;

// 主处理函数
void UsrAI::processData() {
    tagInfo info = getInfo();

    // 初始化检查
    if (!initDone) {
        initialize();
        initDone = true;
        DebugText(string("AI初始化完成"));
    }

    // 更新实时地图
    updateMap(info);

    // 按阶段处理
    switch (stage) {
        case 1: stage1(info); break;
        case 2: stage2(info); break;
        case 3: stage3(info); break;
        case 4: stage4(info); break;
    }

    // 通用管理任务
    assignIdleVillagers(info);
    manageVillagerTasks(info);
    manageFishingBoats(info);
    manageMilitaryProduction(info);
}

// 初始化函数
void UsrAI::initialize() {
    // 初始化地图
    for (int i = 0; i < 128; i++) {
        for (int j = 0; j < 128; j++) {
            map[i][j] = ' ';
        }
    }

    // 重置状态变量
    enemyMainlandFound = false;
    farmsBuilt = 0;
    warshipsBuilt = 0;
    transportShipsBuilt = 0;
    lastVillagerProductionTime = 0;
    lastWarshipProductionTime = 0;

    // 清空任务列表
    idleVillagers.clear();
    woodCutters.clear();
    fishermen.clear();
    builders.clear();
    hunters.clear();
    warships.clear();
    transportShips.clear();
    militaryUnits.clear();
}

void UsrAI::updateMap(tagInfo& info) {
    // 消除未使用参数警告
    (void)info;

    // 重置地图显示
    for (int i = 0; i < 128; i++) {
        for (int j = 0; j < 128; j++) {
            // 正确访问二维地图数组 - 使用解引用指针方式
            if ((*info.theMap)[i][j].type == MAPPATTERN_OCEAN) {
                map[i][j] = '~'; // 海洋
            } else if ((*info.theMap)[i][j].type == MAPPATTERN_GRASS) {
                map[i][j] = '.'; // 草地
            } else {
                map[i][j] = '?'; // 未知区域
            }
        }
    }

    // 标记资源 - 使用正确的坐标成员BlockDR和BlockUR
    for (const auto& resource : info.resources) {
        char symbol = 'R';
        if (resource.Type == RESOURCE_TREE) symbol = 'T';
        else if (resource.Type == RESOURCE_STONE) symbol = 'S';
        else if (resource.Type == RESOURCE_GAZELLE) symbol = 'A';
        else if (resource.Type == RESOURCE_GOLD) symbol = 'G';
        else if (resource.Type == RESOURCE_FISH) symbol = 'F';

        // 使用结构体中实际存在的BlockDR和BlockUR成员
        map[resource.BlockDR][resource.BlockUR] = symbol;
    }

    // 标记建筑 - 使用正确的坐标成员BlockDR和BlockUR
    for (const auto& building : info.buildings) {
        char symbol = 'B';
        if (building.Type == BUILDING_CENTER) symbol = 'C';
        else if (building.Type == BUILDING_DOCK) symbol = 'D';
        else if (building.Type == BUILDING_HOME) symbol = 'H';
        else if (building.Type == BUILDING_ARMYCAMP) symbol = 'M'; // 兵营
        else if (building.Type == BUILDING_RANGE) symbol = 'R';    // 靶场

        // 使用结构体中实际存在的BlockDR和BlockUR成员
        map[building.BlockDR][building.BlockUR] = symbol;
    }
}
// 分配空闲村民
void UsrAI::assignIdleVillagers(tagInfo& info) {
    idleVillagers.clear();
    for (const auto& farmer : info.farmers) {
        if (farmer.FarmerSort == VILLAGER && farmer.NowState == HUMAN_STATE_IDLE) {
            idleVillagers.push_back(farmer.SN);
        }
    }
}

// 管理村民任务
void UsrAI::manageVillagerTasks(tagInfo& info) {
    if (idleVillagers.empty()) return;
    int sn = idleVillagers.back();

    // 检查是否有建筑需要建造（优先石器时代建房屋）
    if (stage == 1 && info.buildings.size() < 5) {
        if (tryBuildBuilding(BUILDING_HOME, 30, 4)) {
            builders.push_back(sn);
            return;
        }
    }

    // 阶段1：优先食物和木材采集
    if (stage == 1) {
        // 食物严重不足时优先组队猎杀大象
        if (info.Meat < 150) {
            int elephantSN = findClosestResource(info, info.farmers[0].DR, info.farmers[0].UR, RESOURCE_ELEPHANT);
            if (elephantSN != -1 && idleVillagers.size() >= 4) {
                huntElephant(info, elephantSN);
                return;
            }
        }

        // 打猎：优先采集羚羊
        if (info.Meat < 300 && hunters.size() < 4) {
            int gazelleSN = findClosestResource(info, info.farmers[0].DR, info.farmers[0].UR, RESOURCE_GAZELLE);
            if (gazelleSN != -1) {
                HumanAction(sn, gazelleSN);
                hunters.push_back(sn);
                idleVillagers.pop_back();
                DebugText(string("村民") + to_string(sn) + string("去打猎"));
                return;
            }
        }

        // 木材采集：确保有足够的村民砍树
        if (woodCutters.size() < 6) {
            int treeSN = findClosestResource(info, info.farmers[0].DR, info.farmers[0].UR, RESOURCE_TREE);
            if (treeSN != -1) {
                HumanAction(sn, treeSN);
                woodCutters.push_back(sn);
                idleVillagers.pop_back();
                DebugText(string("村民") + to_string(sn) + string("去砍树"));
                return;
            }
        }
    }

    // 阶段2及以上：农田工作
    if (stage >= 2) {
        for (const auto& building : info.buildings) {
            if (building.Type == BUILDING_FARM && building.Cnt > 0) {
                bool isOccupied = false;
                for (const auto& farmer : info.farmers) {
                    if (farmer.WorkObjectSN == building.SN) {
                        isOccupied = true;
                        break;
                    }
                }
                if (!isOccupied) {
                    HumanAction(sn, building.SN);
                    idleVillagers.pop_back();
                    DebugText(string("村民") + to_string(sn) + string("去种田"));
                    return;
                }
            }
        }
    }

    // 若未分配其他任务，则检查是否有猎物遗留需要采集
    assignCollectPrey(info);
}

// 管理渔船
void UsrAI::manageFishingBoats(tagInfo& info) {
    for (const auto& farmer : info.farmers) {
        if (farmer.FarmerSort == FISHING_BOAT && farmer.NowState == HUMAN_STATE_IDLE) {
            int fishSN = findClosestResource(info, farmer.DR, farmer.UR, RESOURCE_FISH);
            if (fishSN != -1) {
                HumanAction(farmer.SN, fishSN);
            }
        }
    }
}

// 管理军事生产
void UsrAI::manageMilitaryProduction(tagInfo& info) {
    if (stage >= 2) {
        for (const auto& building : info.buildings) {
            if (building.Type == BUILDING_DOCK && building.Project == 0) {
                if (warshipsBuilt < MAX_WARSHIPS && info.Wood >= 60) {
                    BuildingAction(building.SN, BUILDING_DOCK_CREATE_SHIP);
                    warshipsBuilt++;
                }
            }
        }
    }
}

// 阶段1：石器时代发展
void UsrAI::stage1(tagInfo& info) {
    // 生产村民
    if ((int)info.farmers.size() < MAX_VILLAGERS && info.Meat >= 50 && info.GameFrame - lastVillagerProductionTime > 600) {
        for (const auto& building : info.buildings) {
            if (building.Type == BUILDING_CENTER && building.Project == 0) {
                BuildingAction(building.SN, BUILDING_CENTER_CREATEFARMER);
                lastVillagerProductionTime = info.GameFrame;
                DebugText(string("生产村民中..."));
                break;
            }
        }
    }

    // 建造船坞
    if (!tryBuildBuilding(BUILDING_DOCK, 120, 1)) {
        DebugText(string("正在建造船坞..."));
    }

    // 建造房屋（确保人口空间充足）
    if (info.Human_MaxNum - (int)info.farmers.size() < 3) {
        tryBuildBuilding(BUILDING_HOME, 30, 4);
    }

    // 建造仓库
    buildWarehouse(info);

    // 生产渔船
    for (const auto& building : info.buildings) {
        if (building.Type == BUILDING_DOCK && building.Project == 0) {
            if ((int)info.farmers.size() < MAX_FISHING_BOATS && info.Wood >= 60) {
                BuildingAction(building.SN, BUILDING_DOCK_CREATE_SAILING);
                DebugText(string("生产渔船中..."));
            }
        }
    }

    // 检查升级条件
    if (info.Meat >= 500 && info.buildings.size() >= 3) {
        for (const auto& building : info.buildings) {
            if (building.Type == BUILDING_CENTER && building.Project == 0) {
                BuildingAction(building.SN, BUILDING_CENTER_UPGRADE);
                DebugText(string("升级到工具时代..."));
                stage = 2;
                break;
            }
        }
    }
}

// 阶段2：工具时代发展
void UsrAI::stage2(tagInfo& info) {
    // 建造市场
    if (!tryBuildBuilding(BUILDING_MARKET, 150, 1)) {
        DebugText(string("正在建造市场..."));
    }

    // 建造农田（上限6块）
    if (farmsBuilt < 6 && tryBuildBuilding(BUILDING_FARM, 75, 6, BUILDING_MARKET)) {
        farmsBuilt++;
    }

    // 研究科技（以伐木技术为例）
    for (const auto& building : info.buildings) {
        if (building.Type == BUILDING_MARKET && building.Project == 0) {
            if (info.Wood >= 75 && info.Meat >= 120) {
                BuildingAction(building.SN, BUILDING_MARKET_WOOD_UPGRADE);
                DebugText(string("升级伐木技术..."));
            }
        }
    }

    // 探索海洋
    exploreOcean(info);

    // 检查是否发现敌方大陆并海军就绪
    if (enemyMainlandFound && warshipsBuilt >= 4) {
        stage = 3;
        DebugText(string("发现敌方大陆，准备登陆！"));
    }
}

// 阶段3：登陆敌方大陆
void UsrAI::stage3(tagInfo& info) {
    // 生产运输船（1艘）
    if (transportShipsBuilt == 0) {
        for (const auto& building : info.buildings) {
            if (building.Type == BUILDING_DOCK && building.Project == 0) {
                if (info.Wood >= 60) {
                    BuildingAction(building.SN, BUILDING_DOCK_CREATE_WOOD_BOAT);
                    transportShipsBuilt++;
                    DebugText(string("生产运输船..."));
                }
            }
        }
    }

    // 装载村民上运输船
    for (const auto& ship : info.farmers) {
        if (ship.FarmerSort == TRANSPORT_SHIP && ship.Resource < 5) {
            for (const auto& villager : info.farmers) {
                if (villager.FarmerSort == VILLAGER && villager.NowState == HUMAN_STATE_IDLE) {
                    HumanAction(villager.SN, ship.SN);
                    DebugText(string("装载村民..."));
                    break;
                }
            }
        }
    }

    // 运输船航行前往敌方大陆
    for (const auto& ship : info.farmers) {
        if (ship.FarmerSort == TRANSPORT_SHIP && ship.NowState == HUMAN_STATE_IDLE && enemyMainlandFound) {
            HumanMove(ship.SN, enemyMainlandX, enemyMainlandY);
            DebugText(string("运输船前往敌方大陆..."));
        }
    }

    // 检查登陆情况
    for (const auto& farmer : info.farmers) {
        if (farmer.BlockDR >= enemyMainlandX - 10 && farmer.BlockDR <= enemyMainlandX + 10 &&
            farmer.BlockUR >= enemyMainlandY - 10 && farmer.BlockUR <= enemyMainlandY + 10 &&
            farmer.FarmerSort == VILLAGER) {
            stage = 4;
            DebugText(string("成功登陆敌方大陆！"));
            break;
        }
    }
}

// 阶段4：敌方大陆发展
void UsrAI::stage4(tagInfo& info) {
    // 在敌方大陆建造军事建筑
    bool barracksBuilt = false;
    bool archeryBuilt = false;
    for (const auto& building : info.buildings) {
        if (building.Type == BUILDING_ARMYCAMP) barracksBuilt = true;
        if (building.Type == BUILDING_RANGE) archeryBuilt = true;
    }
    if (!barracksBuilt) {
        tryBuildBuilding(BUILDING_ARMYCAMP, 125, 1);
    }
    if (!archeryBuilt) {
        tryBuildBuilding(BUILDING_RANGE, 150, 1, BUILDING_ARMYCAMP);
    }

    // 生产军队
    manageMilitaryProduction(info);

    // 攻击敌方单位
    attackEnemyUnits(info);

    // 采集金矿
    for (const auto& farmer : info.farmers) {
        if (farmer.FarmerSort == VILLAGER && farmer.NowState == HUMAN_STATE_IDLE) {
            int goldSN = findClosestResource(info, farmer.DR, farmer.UR, RESOURCE_GOLD);
            if (goldSN != -1) {
                HumanAction(farmer.SN, goldSN);
                DebugText(string("采集金矿..."));
            }
        }
    }
}

// 探索海洋以发现敌方大陆
void UsrAI::exploreOcean(tagInfo& info) {
    for (const auto& army : info.armies) {
        if (army.Sort == AT_SHIP && army.NowState == HUMAN_STATE_IDLE) {
            // 简单探索策略：向地图边缘移动
            double targetX = 0, targetY = 0;
            if (army.DR < 64) targetX = 120 * BLOCK_SIZE;
            else targetX = 8 * BLOCK_SIZE;
            if (army.UR < 64) targetY = 120 * BLOCK_SIZE;
            else targetY = 8 * BLOCK_SIZE;
            HumanMove(army.SN, targetX, targetY);

            // 检查是否发现敌方大陆（金矿表示敌方大陆）
            for (int i = 0; i < 128; i++) {
                for (int j = 0; j < 128; j++) {
                    if (map[i][j] == 'G') {
                        enemyMainlandX = i * BLOCK_SIZE;
                        enemyMainlandY = j * BLOCK_SIZE;
                        enemyMainlandFound = true;
                        DebugText(string("发现敌方大陆！"));
                        return;
                    }
                }
            }
        }
    }
}

// 攻击敌方单位或建筑
void UsrAI::attackEnemyUnits(tagInfo& info) {
    for (const auto& army : info.armies) {
        if (army.NowState == HUMAN_STATE_IDLE) {
            int enemySN = findClosestEnemy(info, army.DR, army.UR);
            if (enemySN != -1) {
                HumanAction(army.SN, enemySN);
            }
        }
    }
}
//尝试建造建筑
bool UsrAI::tryBuildBuilding(int buildingType, int requiredWood, int maxCount, int requiredBuilding, bool onlyNearTownCenter) {
    tagInfo info = getInfo();

    // 检查是否已达到建造数量上限
    int count = 0;
    for (const auto& b : info.buildings) {
        if (b.Type == buildingType) count++;
    }
    if (count >= maxCount) return false;

    // 检查前置建筑
    if (requiredBuilding != -1) {
        bool foundReq = false;
        for (const auto& b : info.buildings) {
            if (b.Type == requiredBuilding) {
                foundReq = true;
                break;
            }
        }
        if (!foundReq) return false;
    }

    // 检查资源
    if (info.Wood < requiredWood) return false;

    // 选择一名空闲村民作为建造者
    int builderSN = -1;
    if (!idleVillagers.empty()) {
        builderSN = idleVillagers.back();
        idleVillagers.pop_back();
    } else {
        for (const auto& farmer : info.farmers) {
            if (farmer.FarmerSort == VILLAGER && farmer.NowState == HUMAN_STATE_IDLE) {
                builderSN = farmer.SN;
                break;
            }
        }
        if (builderSN == -1) return false;
    }

    // 确定建造基准位置（默认为己方市镇中心或敌方大陆登陆点）
    int baseDR = 0, baseUR = 0;
    if (onlyNearTownCenter || stage < 3) {
        // 己方基地以市镇中心为中心
        for (const auto& b : info.buildings) {
            if (b.Type == BUILDING_CENTER) {
                baseDR = b.BlockDR;
                baseUR = b.BlockUR;
                break;
            }
        }
    }
    if (stage >= 3) {
        // 敌方大陆以发现的金矿点为中心
        baseDR = (int)(enemyMainlandX / BLOCK_SIZE);
        baseUR = (int)(enemyMainlandY / BLOCK_SIZE);
    }

    // 搜索建造位置
    int placeDR = -1, placeUR = -1;
    if (buildingType == BUILDING_DOCK) {
        // 搜索靠近己方基地的岸边水域
        int maxRadius = 20;
        bool found = false;
        for (int r = 1; r <= maxRadius && !found; ++r) {
            // 环绕搜索半径 r 的边界
            for (int i = baseDR - r; i <= baseDR + r && !found; ++i) {
                int j1 = baseUR - r;
                int j2 = baseUR + r;
                if (i >= 0 && i < 128) {
                    if (j1 >= 0 && j1 < 128 && map[i][j1] == '~') {
                        if (i < 127 && j1 < 127 && map[i+1][j1] == '~' && map[i][j1+1] == '~' && map[i+1][j1+1] == '~') {
                            // 确保相邻有陆地
                            if ((i > 0 && map[i-1][j1] == '.') || (j1 > 0 && map[i][j1-1] == '.') ||
                                (i < 126 && map[i+2][j1] == '.') || (j1 < 126 && map[i][j1+2] == '.')) {
                                placeDR = i;
                                placeUR = j1;
                                found = true;
                                break;
                            }
                        }
                    }
                    if (j2 >= 0 && j2 < 128 && map[i][j2] == '~') {
                        if (i < 127 && j2 < 127 && map[i+1][j2] == '~' && map[i][j2+1] == '~' && map[i+1][j2+1] == '~') {
                            if ((i > 0 && map[i-1][j2] == '.') || (j2 > 0 && map[i][j2-1] == '.') ||
                                (i < 126 && map[i+2][j2] == '.') || (j2 < 126 && map[i][j2+2] == '.')) {
                                placeDR = i;
                                placeUR = j2;
                                found = true;
                                break;
                            }
                        }
                    }
                }
            }
            for (int j = baseUR - r + 1; j < baseUR + r && !found; ++j) {
                int i1 = baseDR - r;
                int i2 = baseDR + r;
                if (j >= 0 && j < 128) {
                    if (i1 >= 0 && i1 < 128 && map[i1][j] == '~') {
                        if (i1 < 127 && j < 127 && map[i1+1][j] == '~' && map[i1][j+1] == '~' && map[i1+1][j+1] == '~') {
                            if ((i1 > 0 && map[i1-1][j] == '.') || (j > 0 && map[i1][j-1] == '.') ||
                                (i1 < 126 && map[i1+2][j] == '.') || (j < 126 && map[i1][j+2] == '.')) {
                                placeDR = i1;
                                placeUR = j;
                                found = true;
                                break;
                            }
                        }
                    }
                    if (i2 >= 0 && i2 < 128 && map[i2][j] == '~') {
                        if (i2 < 127 && j < 127 && map[i2+1][j] == '~' && map[i2][j+1] == '~' && map[i2+1][j+1] == '~') {
                            if ((i2 > 0 && map[i2-1][j] == '.') || (j > 0 && map[i2][j-1] == '.') ||
                                (i2 < 126 && map[i2+2][j] == '.') || (j < 126 && map[i2][j+2] == '.')) {
                                placeDR = i2;
                                placeUR = j;
                                found = true;
                                break;
                            }
                        }
                    }
                }
            }
        }
    } else {
        // 搜索靠近基准点的空地
        int maxRadius = 10;
        bool found = false;
        for (int r = 1; r <= maxRadius && !found; ++r) {
            for (int i = baseDR - r; i <= baseDR + r && !found; ++i) {
                int j1 = baseUR - r;
                int j2 = baseUR + r;
                if (i >= 0 && i < 128) {
                    if (j1 >= 0 && j1 < 128 && map[i][j1] == '.') {
                        if (isPositionValidForBuilding(i, j1, buildingType)) {
                            placeDR = i;
                            placeUR = j1;
                            found = true;
                            break;
                        }
                    }
                    if (j2 >= 0 && j2 < 128 && map[i][j2] == '.') {
                        if (isPositionValidForBuilding(i, j2, buildingType)) {
                            placeDR = i;
                            placeUR = j2;
                            found = true;
                            break;
                        }
                    }
                }
            }
            for (int j = baseUR - r + 1; j < baseUR + r && !found; ++j) {
                int i1 = baseDR - r;
                int i2 = baseDR + r;
                if (j >= 0 && j < 128) {
                    if (i1 >= 0 && i1 < 128 && map[i1][j] == '.') {
                        if (isPositionValidForBuilding(i1, j, buildingType)) {
                            placeDR = i1;
                            placeUR = j;
                            found = true;
                            break;
                        }
                    }
                    if (i2 >= 0 && i2 < 128 && map[i2][j] == '.') {
                        if (isPositionValidForBuilding(i2, j, buildingType)) {
                            placeDR = i2;
                            placeUR = j;
                            found = true;
                            break;
                        }
                    }
                }
            }
        }
    }

    if (placeDR == -1 || placeUR == -1) {
        return false;
    }

    // 发出建造指令
    HumanBuild(builderSN, buildingType, placeDR, placeUR);
    return true;
}

// 查找最近的指定资源
int UsrAI::findClosestResource(tagInfo& info, double dr, double ur, int resourceType) {
    int closestSN = -1;
    double minDist = 1e9;
    for (const auto& resource : info.resources) {
        if (resource.Type == resourceType && resource.Cnt > 0) {
            double dist = pow(resource.DR - dr, 2) + pow(resource.UR - ur, 2);
            if (dist < minDist) {
                minDist = dist;
                closestSN = resource.SN;
            }
        }
    }
    return closestSN;
}

// 查找最近的敌方单位或建筑
int UsrAI::findClosestEnemy(tagInfo& info, double dr, double ur) {
    int closestSN = -1;
    double minDist = 1e9;
    for (const auto& enemy : info.enemy_armies) {
        double dist = pow(enemy.DR - dr, 2) + pow(enemy.UR - ur, 2);
        if (dist < minDist) {
            minDist = dist;
            closestSN = enemy.SN;
        }
    }
    for (const auto& building : info.enemy_buildings) {
        double dist = pow(building.BlockDR * BLOCK_SIZE - dr, 2) +
                      pow(building.BlockUR * BLOCK_SIZE - ur, 2);
        if (dist < minDist) {
            minDist = dist;
            closestSN = building.SN;
        }
    }
    return closestSN;
}

// 判断建筑放置位置是否有效
bool UsrAI::isPositionValidForBuilding(int blockDR, int blockUR, int buildingType) {
    int size = (buildingType == BUILDING_HOME || buildingType == BUILDING_DOCK || buildingType == BUILDING_ARROWTOWER) ? 2 : 3;
    if (blockDR < 0 || blockUR < 0 || blockDR + size > 128 || blockUR + size > 128) {
        return false;
    }
    for (int i = blockDR; i < blockDR + size; ++i) {
        for (int j = blockUR; j < blockUR + size; ++j) {
            char c = map[i][j];
            if (c == '~' || c == '?') return false;
            if (c == 'T' || c == 'S' || c == 'A' || c == 'G' || c == 'F' || c == 'R') return false;
            if (c == 'B' || c == 'C' || c == 'D' || c == 'H') return false;
        }
    }
    return true;
}

// 组建村民狩猎队伍
void UsrAI::assignHuntingTeam(tagInfo& info, int targetSN, int teamSize, int resourceType) {
    if ((int)idleVillagers.size() < teamSize) return;
    vector<int> team;
    for (int i = 0; i < teamSize && !idleVillagers.empty(); ++i) {
        team.push_back(idleVillagers.back());
        idleVillagers.pop_back();
    }
    for (int sn : team) {
        HumanAction(sn, targetSN);
        hunters.push_back(sn);
    }
    string prey = (resourceType == RESOURCE_ELEPHANT ? "大象" : (resourceType == RESOURCE_GAZELLE ? "羚羊" : "猎物"));
    DebugText(string("组建") + to_string(teamSize) + string("人狩猎队攻击") + prey);
}

// 建造仓库（储藏坑）靠近主要资源点
void UsrAI::buildWarehouse(tagInfo& info) {
    bool hasWarehouse = false;
    for (const auto& b : info.buildings) {
        if (b.Type == BUILDING_STOCK) {
            hasWarehouse = true;
            break;
        }
    }
    if (!hasWarehouse && info.Wood >= 120) {
        if (!tryBuildBuilding(BUILDING_STOCK, 120, 1)) {
            DebugText(string("正在建造仓库..."));
        }
    }
}

// 分配村民采集猎物遗留资源
void UsrAI::assignCollectPrey(tagInfo& info) {
    for (const auto& resource : info.resources) {
        if ((resource.Type == RESOURCE_GAZELLE || resource.Type == RESOURCE_ELEPHANT) && resource.Cnt > 0) {
            // 检查是否已有村民在采集该猎物
            int collectors = 0;
            for (const auto& farmer : info.farmers) {
                if (farmer.WorkObjectSN == resource.SN) collectors++;
            }
            int maxCollectors = (resource.Type == RESOURCE_ELEPHANT ? 4 : 2);
            if (collectors < maxCollectors && !idleVillagers.empty()) {
                int sn = idleVillagers.back();
                idleVillagers.pop_back();
                HumanAction(sn, resource.SN);
                DebugText(string("村民") + to_string(sn) + string("采集猎物"));
                return;
            }
        }
    }
}

// 大象狩猎策略（组队猎杀）
void UsrAI::huntElephant(tagInfo& info, int elephantSN) {
    if ((int)idleVillagers.size() < 4) return;
    assignHuntingTeam(info, elephantSN, 4, RESOURCE_ELEPHANT);
}

